package parser;
import java.util.ArrayList;
import java.util.List;

import javax.swing.table.DefaultTableModel;

import net.sf.jsqlparser.expression.AllComparisonExpression;
import net.sf.jsqlparser.expression.AnyComparisonExpression;
import net.sf.jsqlparser.expression.CaseExpression;
import net.sf.jsqlparser.expression.DateValue;
import net.sf.jsqlparser.expression.DoubleValue;
import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.expression.ExpressionVisitor;
import net.sf.jsqlparser.expression.Function;
import net.sf.jsqlparser.expression.InverseExpression;
import net.sf.jsqlparser.expression.JdbcParameter;
import net.sf.jsqlparser.expression.LongValue;
import net.sf.jsqlparser.expression.NullValue;
import net.sf.jsqlparser.expression.Parenthesis;
import net.sf.jsqlparser.expression.StringValue;
import net.sf.jsqlparser.expression.TimeValue;
import net.sf.jsqlparser.expression.TimestampValue;
import net.sf.jsqlparser.expression.WhenClause;
import net.sf.jsqlparser.expression.operators.arithmetic.Addition;
import net.sf.jsqlparser.expression.operators.arithmetic.Concat;
import net.sf.jsqlparser.expression.operators.arithmetic.Division;
import net.sf.jsqlparser.expression.operators.arithmetic.Multiplication;
import net.sf.jsqlparser.expression.operators.arithmetic.Subtraction;
import net.sf.jsqlparser.expression.operators.conditional.AndExpression;
import net.sf.jsqlparser.expression.operators.conditional.OrExpression;
import net.sf.jsqlparser.expression.operators.relational.Between;
import net.sf.jsqlparser.expression.operators.relational.EqualsTo;
import net.sf.jsqlparser.expression.operators.relational.ExistsExpression;
import net.sf.jsqlparser.expression.operators.relational.GreaterThan;
import net.sf.jsqlparser.expression.operators.relational.GreaterThanEquals;
import net.sf.jsqlparser.expression.operators.relational.InExpression;
import net.sf.jsqlparser.expression.operators.relational.IsNullExpression;
import net.sf.jsqlparser.expression.operators.relational.LikeExpression;
import net.sf.jsqlparser.expression.operators.relational.MinorThan;
import net.sf.jsqlparser.expression.operators.relational.MinorThanEquals;
import net.sf.jsqlparser.expression.operators.relational.NotEqualsTo;
import net.sf.jsqlparser.schema.Column;
import net.sf.jsqlparser.statement.select.SubSelect;

public class WhereExpressionParser implements ExpressionVisitor
{
	private DefaultTableModel model;
	private List<Integer> rowList;
	private Object value;
	public List<Integer> getRow(Expression expression,DefaultTableModel model)
	{
		this.model = model;
		rowList = new ArrayList<Integer>();
		expression.accept(this);
		return rowList;
		
	}
	public Object getValue(Expression expression)
	{
		expression.accept(this);
		return value;
	}
	
	public static String doFunction(Function function,DefaultTableModel model)
	{
		String result=null;
		String selectedRow=null;
		int index;
		if (!function.isAllColumns()) 
		{
			selectedRow=function.getParameters().getExpressions().get(0).toString();
			if (SelectParser.getTable(selectedRow)==null) 
				selectedRow = SelectParser.getTable(model.getColumnName(0))+'.'+selectedRow;
		}
		if (function.getName().equalsIgnoreCase("MAX"))
		{
			if (selectedRow==null) result=null;
			else
			{
				index = model.findColumn(selectedRow);
				String maxValue;
				if (model.getValueAt(0, index)!=null) maxValue = model.getValueAt(0, index).toString();
				else maxValue=null;
				for (int i=1;i<model.getRowCount();i++) 
				{
					String value;
					if (model.getValueAt(i, index)!=null && model.getValueAt(i, index)!="") 
					{
						value = model.getValueAt(i, index).toString();
						if (maxValue!=null) 
						{
							if (SelectParser.compareValue(value, maxValue)>0) maxValue = value;
						}
						else maxValue = value;
					}
				}
				result=maxValue;
			}
		}
		else if (function.getName().equalsIgnoreCase("MIN"))
		{
			if (selectedRow==null) result=null;
			else
			{
				index = model.findColumn(selectedRow);
				String minValue;
				if (model.getValueAt(0, index)!=null) minValue = model.getValueAt(0, index).toString();
				else minValue=null;
				for (int i=1;i<model.getRowCount();i++) 
				{
					String value;
					if (model.getValueAt(i, index)!=null && model.getValueAt(i, index)!="") 
					{
						value = model.getValueAt(i, index).toString();
						if (minValue!=null) 
						{
							if (SelectParser.compareValue(value, minValue)<0) minValue = value;
						}
						else minValue = value;
					}
				}
				result=minValue;
			}
		}
		else if (function.getName().equalsIgnoreCase("COUNT"))
		{
			if (selectedRow==null) result= String.valueOf(model.getRowCount());
			else
			{
				index = model.findColumn(selectedRow);
				int count=0;
				for (int i=0;i<model.getRowCount();i++) 
				{
					if (model.getValueAt(i, index)!=null  && model.getValueAt(i, index)!="") count++;
				}
				result= String.valueOf(count);
			}
		}
		else if (function.getName().equalsIgnoreCase("SUM"))
		{
			if (selectedRow==null) result=null;
			else
			{
				index = model.findColumn(selectedRow);
				int sum=0;
				for (int i=0;i<model.getRowCount();i++) 
				{
					String value;
					if (model.getValueAt(i, index)!=null  && model.getValueAt(i, index)!="") 
					{
						value = model.getValueAt(i, index).toString();
						sum +=Integer.parseInt(value);
					}
				}
				result= String.valueOf(sum);
			}
		}
		else if (function.getName().equalsIgnoreCase("AVG"))
		{
			if (selectedRow==null) result=null;
			else
			{
				index = model.findColumn(selectedRow);
				int sum=0,count=0;
				for (int i=0;i<model.getRowCount();i++) 
				{
					String value;
					if (model.getValueAt(i, index)!=null  && model.getValueAt(i, index)!="") 
					{
						value = model.getValueAt(i, index).toString();
						sum +=Integer.parseInt(value);
						count++;
					}
				}
				result = String.valueOf(sum/count);
			}
		}
		else if (function.getName().equalsIgnoreCase("FIRST"))
		{
			if (selectedRow==null) result=null;
			else
			{
				index = model.findColumn(selectedRow);
				for (int i=0;i<model.getRowCount();i++) 
				{
					String value;
					if (model.getValueAt(i, index)!=null  && model.getValueAt(i, index)!="") 
					{
						value = model.getValueAt(i, index).toString();
						result = value;
						break;
					}
				}
			}
		}
		else if (function.getName().equalsIgnoreCase("LAST"))
		{
			if (selectedRow==null) result=null;
			else
			{
				index = model.findColumn(selectedRow);
				for (int i=model.getRowCount()-1;i>=0;i--) 
				{
					String value;
					if (model.getValueAt(i, index)!=null  && model.getValueAt(i, index)!="") 
					{
						value = model.getValueAt(i, index).toString();
						result = value;
						break;
					}
				}
			}
		}
		return result;
	}
	@Override
	public void visit(NullValue arg0) 
	{
		// TODO Auto-generated method stub
		
	}
	@Override
	public void visit(Function function) 
	{
		value = function;
	}
	@Override
	public void visit(InverseExpression arg0) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void visit(JdbcParameter arg0) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void visit(DoubleValue doubleValue) 
	{
	}
	@Override
	public void visit(LongValue longValue) 
	{
		value = longValue.getStringValue();
	}
	@Override
	public void visit(DateValue arg0) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void visit(TimeValue arg0) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void visit(TimestampValue arg0) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void visit(Parenthesis parenthesis) 
	{
		WhereExpressionParser parser = new WhereExpressionParser();
		rowList = parser.getRow(parenthesis.getExpression(), model);
	}
	@Override
	public void visit(StringValue stringValue) 
	{
		value=stringValue.getValue();
	}
	@Override
	public void visit(Addition arg0) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void visit(Division arg0) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void visit(Multiplication arg0) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void visit(Subtraction arg0) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void visit(AndExpression andExpression) 
	{
		WhereExpressionParser parser = new WhereExpressionParser();
		List<Integer> row1,row2;
		row1 = parser.getRow(andExpression.getLeftExpression(),model);
		row2 = parser.getRow(andExpression.getRightExpression(),model);
		for (int i=0;i<row1.size();i++)
			for (int j=0;j<row2.size();j++) if (row1.get(i).equals(row2.get(j))) rowList.add(row1.get(i));
	}
	@Override
	public void visit(OrExpression orExpression) 
	{
		WhereExpressionParser parser = new WhereExpressionParser();
		List<Integer> row1,row2;
		row1 = parser.getRow(orExpression.getLeftExpression(),model);
		row2 = parser.getRow(orExpression.getRightExpression(),model);
		for (int i=0;i<row1.size();i++) rowList.add(row1.get(i));
		
		for (int i=0;i<row2.size();i++)
		{
			boolean t=true;
			for (int j=0;j<rowList.size();j++) if (rowList.get(j).equals(row2.get(i))) t=false;
			if (t) rowList.add(row2.get(i));
			
		}
			
	}
	@Override
	public void visit(Between arg0) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void visit(EqualsTo equalsTo) 
	{
		WhereExpressionParser parser = new WhereExpressionParser();
		Object value1 = parser.getValue(equalsTo.getLeftExpression());
		Object value2 = parser.getValue(equalsTo.getRightExpression());
		
		if ((value1 instanceof Column)&&(value2 instanceof Column))
		{
			String columnName1 = ((Column)value1).getWholeColumnName();
			String columnName2 = ((Column)value2).getWholeColumnName();
			if (SelectParser.getTable(columnName1)==null) 
				columnName1 = SelectParser.getTable(model.getColumnName(0))+'.'+columnName1;
			if (SelectParser.getTable(columnName2)==null)
				columnName2 = SelectParser.getTable(model.getColumnName(0))+'.'+columnName2;
			
			for (int i=0;i<model.getRowCount();i++)
			{
				if (model.getValueAt(i, model.findColumn(columnName1))==null) continue;
				if (model.getValueAt(i, model.findColumn(columnName2))==null) continue;
				if (model.getValueAt(i, model.findColumn(columnName1)).equals(model.getValueAt(i, model.findColumn(columnName2))))
				{
					rowList.add(i);
				}
			}
		}
		else if ((value1 instanceof Column)&&(value2 instanceof String))
		{
			String columnName = ((Column)value1).getWholeColumnName();

			if (SelectParser.getTable(columnName)==null) 
				columnName = SelectParser.getTable(model.getColumnName(0))+'.'+columnName;
			String value = (String)value2;
			for (int i=0;i<model.getRowCount();i++)
			{
				if (model.getValueAt(i, model.findColumn(columnName))==null) continue;
				if (value==null) continue;
				if (model.getValueAt(i, model.findColumn(columnName)).equals(value))
				{
					rowList.add(i);
				}
			}
		}
		else if ((value1 instanceof String)&&(value2 instanceof Column))
		{
			String columnName = ((Column)value2).getWholeColumnName();
			String value = (String)value1;

			if (SelectParser.getTable(columnName)==null) 
				columnName = SelectParser.getTable(model.getColumnName(0))+'.'+columnName;
			for (int i=0;i<model.getRowCount();i++)
			{
				if (model.getValueAt(i, model.findColumn(columnName))==null) continue;
				if (value==null) continue;
				if (model.getValueAt(i, model.findColumn(columnName)).equals(value))
				{
					rowList.add(i);
				}
			}
		}
		else if ((value1 instanceof Function)&&(value2 instanceof String))
		{
			String s1 = ((Function)value1).toString();
			String s2;
			if (value2==null) s2=null;
			else s2=value2.toString();
			for (int i=0;i<model.getRowCount();i++)
			{
				if (model.getValueAt(i, model.findColumn(s1))==null) continue;
				if (model.getValueAt(i, model.findColumn(s1)).equals(s2))
				{
					rowList.add(i);
				}
			}
		}
		else if ((value1 instanceof String)&&(value2 instanceof Function))
		{
			String s2 = ((Function)value2).toString();
			String s1;
			if (value1==null) s1=null;
			else s1=value1.toString();
			for (int i=0;i<model.getRowCount();i++)
			{
				if (model.getValueAt(i, model.findColumn(s2))==null) continue;
				if (model.getValueAt(i, model.findColumn(s2)).equals(s1))
				{
					rowList.add(i);
				}
			}
			
			
		}
		
	}
	@Override
	public void visit(GreaterThan greaterThan) 
	{
		WhereExpressionParser parser = new WhereExpressionParser();
		Object value1 = parser.getValue(greaterThan.getLeftExpression());
		Object value2 = parser.getValue(greaterThan.getRightExpression());
		
		if ((value1 instanceof Column)&&(value2 instanceof Column))
		{
			String columnName1 = ((Column)value1).getWholeColumnName();
			String columnName2 = ((Column)value2).getWholeColumnName();

			if (SelectParser.getTable(columnName1)==null) 
				columnName1 = SelectParser.getTable(model.getColumnName(0))+'.'+columnName1;
			if (SelectParser.getTable(columnName2)==null)
				columnName2 = SelectParser.getTable(model.getColumnName(0))+'.'+columnName2;
			for (int i=0;i<model.getRowCount();i++)
			{
				if (model.getValueAt(i, model.findColumn(columnName1))==null) continue;
				if (model.getValueAt(i, model.findColumn(columnName2))==null) continue;
				String s1 = model.getValueAt(i, model.findColumn(columnName1)).toString();
				String s2 = model.getValueAt(i, model.findColumn(columnName2)).toString();
				if (SelectParser.compareValue(s1, s2)>0)
				{
					rowList.add(i);
				}
			}
		}
		else if ((value1 instanceof Column)&&(value2 instanceof String))
		{
			String columnName = ((Column)value1).getWholeColumnName();
			String value = (String)value2;
			if (SelectParser.getTable(columnName)==null) 
				columnName = SelectParser.getTable(model.getColumnName(0))+'.'+columnName;
			for (int i=0;i<model.getRowCount();i++)
			{
				if (model.getValueAt(i, model.findColumn(columnName))==null) continue;
				if (value==null) continue;
				String s = model.getValueAt(i, model.findColumn(columnName)).toString();
				if (SelectParser.compareValue(s, value)>0)
				{
					rowList.add(i);
				}
			}
		}
		else if ((value1 instanceof String)&&(value2 instanceof Column))
		{
			String columnName = ((Column)value2).getWholeColumnName();
			String value = (String)value1;
			if (SelectParser.getTable(columnName)==null) 
				columnName = SelectParser.getTable(model.getColumnName(0))+'.'+columnName;
			for (int i=0;i<model.getRowCount();i++)
			{
				if (model.getValueAt(i, model.findColumn(columnName))==null) continue;
				if (value==null) continue;
				String s = model.getValueAt(i, model.findColumn(columnName)).toString();
				if (SelectParser.compareValue(s, value)<0)
				{
					rowList.add(i);
				}
			}
		}
		else if ((value1 instanceof Function)&&(value2 instanceof String))
		{
			String s1 = ((Function)value1).toString();
			String s2;
			if (value2==null) s2=null;
			else s2=value2.toString();
			for (int i=0;i<model.getRowCount();i++)
			{
				if (model.getValueAt(i, model.findColumn(s1))==null) continue;
				if (SelectParser.compareValue(model.getValueAt(i, model.findColumn(s1)).toString(), s2)>0)
				{
					rowList.add(i);
				}
			}
		}
		else if ((value1 instanceof String)&&(value2 instanceof Function))
		{
			String s2 = ((Function)value2).toString();
			String s1;
			if (value1==null) s1=null;
			else s1=value1.toString();
			for (int i=0;i<model.getRowCount();i++)
			{
				if (model.getValueAt(i, model.findColumn(s2))==null) continue;
				if (SelectParser.compareValue(model.getValueAt(i, model.findColumn(s2)).toString(), s1)<0)
				{
					rowList.add(i);
				}
			}
			
			
		}
	}
	@Override
	public void visit(GreaterThanEquals greaterThanEquals) 
	{
		WhereExpressionParser parser = new WhereExpressionParser();
		Object value1 = parser.getValue(greaterThanEquals.getLeftExpression());
		Object value2 = parser.getValue(greaterThanEquals.getRightExpression());
		
		if ((value1 instanceof Column)&&(value2 instanceof Column))
		{
			String columnName1 = ((Column)value1).getWholeColumnName();
			String columnName2 = ((Column)value2).getWholeColumnName();

			if (SelectParser.getTable(columnName1)==null) 
				columnName1 = SelectParser.getTable(model.getColumnName(0))+'.'+columnName1;
			if (SelectParser.getTable(columnName2)==null)
				columnName2 = SelectParser.getTable(model.getColumnName(0))+'.'+columnName2;
			for (int i=0;i<model.getRowCount();i++)
			{
				if (model.getValueAt(i, model.findColumn(columnName1))==null) continue;
				if (model.getValueAt(i, model.findColumn(columnName2))==null) continue;
				String s1 = model.getValueAt(i, model.findColumn(columnName1)).toString();
				String s2 = model.getValueAt(i, model.findColumn(columnName2)).toString();
				if (SelectParser.compareValue(s1, s2)>=0)
				{
					rowList.add(i);
				}
			}
		}
		else if ((value1 instanceof Column)&&(value2 instanceof String))
		{
			String columnName = ((Column)value1).getWholeColumnName();
			String value = (String)value2;
			if (SelectParser.getTable(columnName)==null) 
				columnName = SelectParser.getTable(model.getColumnName(0))+'.'+columnName;
			for (int i=0;i<model.getRowCount();i++)
			{
				if (model.getValueAt(i, model.findColumn(columnName))==null) continue;
				if (value==null) continue;
				String s = model.getValueAt(i, model.findColumn(columnName)).toString();
				if (SelectParser.compareValue(s, value)>=0)
				{
					rowList.add(i);
				}
			}
		}
		else if ((value1 instanceof String)&&(value2 instanceof Column))
		{
			String columnName = ((Column)value2).getWholeColumnName();
			String value = (String)value1;
			if (SelectParser.getTable(columnName)==null) 
				columnName = SelectParser.getTable(model.getColumnName(0))+'.'+columnName;
			for (int i=0;i<model.getRowCount();i++)
			{
				if (model.getValueAt(i, model.findColumn(columnName))==null) continue;
				if (value==null) continue;
				String s = model.getValueAt(i, model.findColumn(columnName)).toString();
				if (SelectParser.compareValue(s, value)<=0)
				{
					rowList.add(i);
				}
			}
		}
		else if ((value1 instanceof Function)&&(value2 instanceof String))
		{
			String s1 = ((Function)value1).toString();
			String s2;
			if (value2==null) s2=null;
			else s2=value2.toString();
			for (int i=0;i<model.getRowCount();i++)
			{
				if (model.getValueAt(i, model.findColumn(s1))==null) continue;
				if (SelectParser.compareValue(model.getValueAt(i, model.findColumn(s1)).toString(), s2)>=0)
				{
					rowList.add(i);
				}
			}
		}
		else if ((value1 instanceof String)&&(value2 instanceof Function))
		{
			String s2 = ((Function)value2).toString();
			String s1;
			if (value1==null) s1=null;
			else s1=value1.toString();
			for (int i=0;i<model.getRowCount();i++)
			{
				if (model.getValueAt(i, model.findColumn(s2))==null) continue;
				if (SelectParser.compareValue(model.getValueAt(i, model.findColumn(s2)).toString(), s1)<=0)
				{
					rowList.add(i);
				}
			}
			
			
		}
	}
	@Override
	public void visit(InExpression arg0) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void visit(IsNullExpression arg0) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void visit(LikeExpression arg0) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void visit(MinorThan minorThan) 
	{
		WhereExpressionParser parser = new WhereExpressionParser();
		Object value1 = parser.getValue(minorThan.getLeftExpression());
		Object value2 = parser.getValue(minorThan.getRightExpression());
		
		if ((value1 instanceof Column)&&(value2 instanceof Column))
		{
			
			String columnName1 = ((Column)value1).getWholeColumnName();
			String columnName2 = ((Column)value2).getWholeColumnName();

			if (SelectParser.getTable(columnName1)==null) 
				columnName1 = SelectParser.getTable(model.getColumnName(0))+'.'+columnName1;
			if (SelectParser.getTable(columnName2)==null)
				columnName2 = SelectParser.getTable(model.getColumnName(0))+'.'+columnName2;
			for (int i=0;i<model.getRowCount();i++)
			{
				if (model.getValueAt(i, model.findColumn(columnName1))==null) continue;
				if (model.getValueAt(i, model.findColumn(columnName2))==null) continue;
				String s1 = model.getValueAt(i, model.findColumn(columnName1)).toString();
				String s2 = model.getValueAt(i, model.findColumn(columnName2)).toString();
				if (SelectParser.compareValue(s1, s2)<0)
				{
					rowList.add(i);
				}
			}
		}
		else if ((value1 instanceof Column)&&(value2 instanceof String))
		{
			String columnName = ((Column)value1).getWholeColumnName();
			String value = (String)value2;
			if (SelectParser.getTable(columnName)==null) 
				columnName = SelectParser.getTable(model.getColumnName(0))+'.'+columnName;
			for (int i=0;i<model.getRowCount();i++)
			{
				if (model.getValueAt(i, model.findColumn(columnName))==null) continue;
				if (value==null) continue;
				String s = model.getValueAt(i, model.findColumn(columnName)).toString();
				if (SelectParser.compareValue(s, value)<0)
				{
					rowList.add(i);
				}
			}
		}
		else if ((value1 instanceof String)&&(value2 instanceof Column))
		{
			String columnName = ((Column)value2).getWholeColumnName();
			String value = (String)value1;
			if (SelectParser.getTable(columnName)==null) 
				columnName = SelectParser.getTable(model.getColumnName(0))+'.'+columnName;
			for (int i=0;i<model.getRowCount();i++)
			{
				if (model.getValueAt(i, model.findColumn(columnName))==null) continue;
				if (value==null) continue;
				String s = model.getValueAt(i, model.findColumn(columnName)).toString();
				if (SelectParser.compareValue(s, value)>0)
				{
					rowList.add(i);
				}
			}
		}
		else if ((value1 instanceof Function)&&(value2 instanceof String))
		{
			String s1 = ((Function)value1).toString();
			String s2;
			if (value2==null) s2=null;
			else s2=value2.toString();
			for (int i=0;i<model.getRowCount();i++)
			{
				if (model.getValueAt(i, model.findColumn(s1))==null) continue;
				if (SelectParser.compareValue(model.getValueAt(i, model.findColumn(s1)).toString(), s2)<0)
				{
					rowList.add(i);
				}
			}
		}
		else if ((value1 instanceof String)&&(value2 instanceof Function))
		{
			String s2 = ((Function)value2).toString();
			String s1;
			if (value1==null) s1=null;
			else s1=value1.toString();
			for (int i=0;i<model.getRowCount();i++)
			{
				if (model.getValueAt(i, model.findColumn(s2))==null) continue;
				if (SelectParser.compareValue(model.getValueAt(i, model.findColumn(s2)).toString(), s1)>0)
				{
					rowList.add(i);
				}
			}
			
			
		}
	}
	@Override
	public void visit(MinorThanEquals minorThanEquals) 
	{
		WhereExpressionParser parser = new WhereExpressionParser();
		Object value1 = parser.getValue(minorThanEquals.getLeftExpression());
		Object value2 = parser.getValue(minorThanEquals.getRightExpression());
		
		if ((value1 instanceof Column)&&(value2 instanceof Column))
		{
			String columnName1 = ((Column)value1).getWholeColumnName();
			String columnName2 = ((Column)value2).getWholeColumnName();

			if (SelectParser.getTable(columnName1)==null) 
				columnName1 = SelectParser.getTable(model.getColumnName(0))+'.'+columnName1;
			if (SelectParser.getTable(columnName2)==null)
				columnName2 = SelectParser.getTable(model.getColumnName(0))+'.'+columnName2;
			for (int i=0;i<model.getRowCount();i++)
			{
				if (model.getValueAt(i, model.findColumn(columnName1))==null) continue;
				if (model.getValueAt(i, model.findColumn(columnName2))==null) continue;
				String s1 = model.getValueAt(i, model.findColumn(columnName1)).toString();
				String s2 = model.getValueAt(i, model.findColumn(columnName2)).toString();
				if (SelectParser.compareValue(s1, s2)<=0)
				{
					rowList.add(i);
				}
			}
		}
		else if ((value1 instanceof Column)&&(value2 instanceof String))
		{
			String columnName = ((Column)value1).getWholeColumnName();
			String value = (String)value2;
			if (SelectParser.getTable(columnName)==null) 
				columnName = SelectParser.getTable(model.getColumnName(0))+'.'+columnName;
			for (int i=0;i<model.getRowCount();i++)
			{
				if (model.getValueAt(i, model.findColumn(columnName))==null) continue;
				if (value==null) continue;
				String s = model.getValueAt(i, model.findColumn(columnName)).toString();
				if (SelectParser.compareValue(s, value)<=0)
				{
					rowList.add(i);
				}
			}
		}
		else if ((value1 instanceof String)&&(value2 instanceof Column))
		{
			String columnName = ((Column)value2).getWholeColumnName();
			String value = (String)value1;
			if (SelectParser.getTable(columnName)==null) 
				columnName = SelectParser.getTable(model.getColumnName(0))+'.'+columnName;
			for (int i=0;i<model.getRowCount();i++)
			{
				if (model.getValueAt(i, model.findColumn(columnName))==null) continue;
				if (value==null) continue;
				String s = model.getValueAt(i, model.findColumn(columnName)).toString();
				if (SelectParser.compareValue(s, value)>=0)
				{
					rowList.add(i);
				}
			}
		}
		else if ((value1 instanceof Function)&&(value2 instanceof String))
		{
			String s1 = ((Function)value1).toString();
			String s2;
			if (value2==null) s2=null;
			else s2=value2.toString();
			for (int i=0;i<model.getRowCount();i++)
			{
				if (model.getValueAt(i, model.findColumn(s1))==null) continue;
				if (SelectParser.compareValue(model.getValueAt(i, model.findColumn(s1)).toString(), s2)<=0)
				{
					rowList.add(i);
				}
			}
		}
		else if ((value1 instanceof String)&&(value2 instanceof Function))
		{
			String s2 = ((Function)value2).toString();
			String s1;
			if (value1==null) s1=null;
			else s1=value1.toString();
			for (int i=0;i<model.getRowCount();i++)
			{
				if (model.getValueAt(i, model.findColumn(s2))==null) continue;
				if (SelectParser.compareValue(model.getValueAt(i, model.findColumn(s2)).toString(), s1)>=0)
				{
					rowList.add(i);
				}
			}
			
			
		}
	}
	@Override
	public void visit(NotEqualsTo notEqualsTo) 
	{
		WhereExpressionParser parser = new WhereExpressionParser();
		Object value1 = parser.getValue(notEqualsTo.getLeftExpression());
		Object value2 = parser.getValue(notEqualsTo.getRightExpression());
		
		if ((value1 instanceof Column)&&(value2 instanceof Column))
		{
			String columnName1 = ((Column)value1).getWholeColumnName();
			String columnName2 = ((Column)value2).getWholeColumnName();

			if (SelectParser.getTable(columnName1)==null) 
				columnName1 = SelectParser.getTable(model.getColumnName(0))+'.'+columnName1;
			if (SelectParser.getTable(columnName2)==null)
				columnName2 = SelectParser.getTable(model.getColumnName(0))+'.'+columnName2;
			for (int i=0;i<model.getRowCount();i++)
			{
				if (model.getValueAt(i, model.findColumn(columnName1))==null) continue;
				if (model.getValueAt(i, model.findColumn(columnName2))==null) continue;
				if (!model.getValueAt(i, model.findColumn(columnName1)).equals(model.getValueAt(i, model.findColumn(columnName2))))
				{
					rowList.add(i);
				}
			}
		}
		else if ((value1 instanceof Column)&&(value2 instanceof String))
		{
			String columnName = ((Column)value1).getWholeColumnName();
			String value = (String)value2;
			if (SelectParser.getTable(columnName)==null) 
				columnName = SelectParser.getTable(model.getColumnName(0))+'.'+columnName;
			for (int i=0;i<model.getRowCount();i++)
			{
				if (model.getValueAt(i, model.findColumn(columnName))==null) continue;
				if (value==null) continue;
				if (!model.getValueAt(i, model.findColumn(columnName)).equals(value))
				{
					rowList.add(i);
				}
			}
		}
		else if ((value1 instanceof String)&&(value2 instanceof Column))
		{
			String columnName = ((Column)value2).getWholeColumnName();
			String value = (String)value1;
			if (SelectParser.getTable(columnName)==null) 
				columnName = SelectParser.getTable(model.getColumnName(0))+'.'+columnName;
			for (int i=0;i<model.getRowCount();i++)
			{
				if (model.getValueAt(i, model.findColumn(columnName))==null) continue;
				if (value==null) continue;
				if (!model.getValueAt(i, model.findColumn(columnName)).equals(value))
				{
					rowList.add(i);
				}
			}
		}
		else if ((value1 instanceof Function)&&(value2 instanceof String))
		{
			String s1 = ((Function)value1).toString();
			String s2;
			if (value2==null) s2=null;
			else s2=value2.toString();
			for (int i=0;i<model.getRowCount();i++)
			{
				if (model.getValueAt(i, model.findColumn(s1))==null) continue;
				if (!model.getValueAt(i, model.findColumn(s1)).equals(s2))
				{
					rowList.add(i);
				}
			}
			
			
		}
		else if ((value1 instanceof String)&&(value2 instanceof Function))
		{
			String s2 = ((Function)value2).toString();
			String s1;
			if (value1==null) s1=null;
			else s1=value1.toString();
			for (int i=0;i<model.getRowCount();i++)
			{
				if (model.getValueAt(i, model.findColumn(s2))==null) continue;
				if (!model.getValueAt(i, model.findColumn(s2)).equals(s1))
				{
					rowList.add(i);
				}
			}
			
			
		}
	}
	@Override
	public void visit(Column column) 
	{
		value = column;
	}
	@Override
	public void visit(SubSelect arg0) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void visit(CaseExpression arg0) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void visit(WhenClause arg0) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void visit(ExistsExpression arg0) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void visit(AllComparisonExpression arg0) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void visit(AnyComparisonExpression arg0) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void visit(Concat arg0) {
		// TODO Auto-generated method stub
		
	}
	
}
